package com.xt.purchasingsystem.controller;

import com.xt.purchasingsystem.config.JwtProperties;
import com.xt.purchasingsystem.model.SysUsers;
import com.xt.purchasingsystem.service.AuthorService;
import com.xt.purchasingsystem.utils.CookieUtils;
import com.xt.purchasingsystem.utils.JwtUtils;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import jdk.nashorn.internal.objects.NativeUint8Array;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author: 老姚
 * Date:  2020/5/22 18:14
 */
@RestController
@RequestMapping("/author")
public class AuthorController {
    @Autowired
    private AuthorService authorService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/accredit")
    public Result accredit(@RequestBody SysUsers sysUsers, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = authorService.accredit(sysUsers);
        if (map == null) {
            return ResultFactory.buildFailResult("用户名或密码错误");
        }
        String token = (String) map.get("token");
        //放入Cookie 中
        CookieUtils.setCookie(request, response, this.jwtProperties.getCookieName(), token, this.jwtProperties.getExpire() * 60);
        return ResultFactory.buildSuccessResult("登录成功", map.get("user"));
    }

    @GetMapping("/authentication")
    public Result authentication(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("PURCHASE_TOKEN")) {
                    try {
                        JwtUtils.getInfoFromToken(cookie.getValue(), this.jwtProperties.getPublicKey());
                        return ResultFactory.buildSuccessResult("已经登录");

                    } catch (Exception e) {
                        e.printStackTrace();
                        return ResultFactory.buildFailResult("没有登录");
                    }
                }

            }
        }
        return ResultFactory.buildFailResult("没有登录");
    }

    @GetMapping("/logOut")
    public Result logOut(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie1 : cookies) {
                if (cookie1.getName().equals("PURCHASE_TOKEN")) {
                    System.out.println("找到了cookie");
                    cookie1.setMaxAge(0);
                    cookie1.setValue("");
                    response.addCookie(cookie1);
                }
            }

        }
        return ResultFactory.buildSuccessResult("登出成功");

    }

}
